package com.ethink.platform.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.order.api.constant.OrderRespCode;
import com.ethink.order.api.info.TobSaleDetailOrderInfo;
import com.ethink.order.api.info.TobSaleOrderInfo;
import com.ethink.order.api.model.TobSaleExcelModel;
import com.ethink.platform.controller.order.info.TobSaleImportInfo;
import com.ethink.warehouse.api.LogisticsTraceApplyOrderFacade;
import com.ethink.warehouse.api.LogisticsTraceDetailOrderFacade;
import com.ethink.warehouse.api.MateInventoryFacade;
import com.ethink.warehouse.api.enums.InventoryTypeEnum;
import com.ethink.warehouse.api.enums.LogisticsTraceDetailOrderTypeEnum;
import com.ethink.warehouse.api.enums.SubjectEnum;
import com.ethink.warehouse.api.info.MateInventoryDetailInfo;
import com.ethink.warehouse.api.request.LogisticsTraceApplyAddRequest;
import com.ethink.warehouse.api.request.MateInventoryLockLogAddRequest;
import com.ethink.warehouse.api.request.MateInventoryQueryKeyInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author default
 * @version 1.0.0
 * @ClassName TobSaleManager.java
 * @Description
 * @createTime 2022年11月17日 19:31:00
 */
@Component
public class TobSaleManager {


    @Autowired
    private ExcelImportManager excelImportManager;

    @Autowired
    private MateInventoryFacade mateInventoryFacade;

    @Autowired
    private LogisticsTraceApplyOrderFacade logisticsTraceApplyOrderFacade;

    @Autowired
    private PublicManager publicManager;

    public List<TobSaleImportInfo> importTobSaleOrder(String subWarehouseCode,
                                                      String saleSubChannel, MultipartFile file) {
        List<TobSaleExcelModel> tobSaleExcelModels = excelImportManager.transferClass(file, TobSaleExcelModel.class);
        List<MateInventoryQueryKeyInfo> keyList = new ArrayList<>();
        tobSaleExcelModels.stream().forEach(
                m -> {
                    MateInventoryQueryKeyInfo keyInfo = new MateInventoryQueryKeyInfo();
                    keyInfo.setSubWarehouseCode(subWarehouseCode);
                    keyInfo.setSaleSubChannel(saleSubChannel);
                    keyInfo.setInventoryType(InventoryTypeEnum.ZP.getIntValue());
                    keyInfo.setMateSku(m.getMateSkuCode());
                    keyInfo.setPurchaseCode(m.getPurchaseCode());
                    keyList.add(keyInfo);
                }
        );
        List<MateInventoryDetailInfo> mateInventoryDetailInfos = mateInventoryFacade.listMateInventoryByKeyList(keyList);
        if (tobSaleExcelModels.size() != mateInventoryDetailInfos.size()) {
            throw new BusinessException(OrderRespCode.MATE_SKU_EXCEL_IS_NOT_EXIST);
        }
        List<TobSaleImportInfo> importInfos = tobSaleExcelModels.stream().map(
                m -> {
                    TobSaleImportInfo importInfo = new TobSaleImportInfo();
                    importInfo.setQty(m.getQty());
                    mateInventoryDetailInfos.stream()
                            .filter(m1 -> StrUtil.equals(m.getMateSkuCode(), m1.getMateSku())
                                    && StrUtil.equals(m.getPurchaseCode(), m1.getPurchaseCode())).forEach(
                            m2 -> {
                                BeanUtil.copyProperties(m2, importInfo);
                            }
                    );
                    //计算毛利率
                    importInfo.setSalePrice(m.getSalePrice());
                    importInfo.setGrossProfitRate(publicManager.grossProfitRate(m.getSalePrice(), importInfo.getCostPrice()));
                    return importInfo;
                }
        ).collect(Collectors.toList());
        return importInfos;
    }

    public void tobApply(TobSaleOrderInfo orderInfo, List<TobSaleDetailOrderInfo> tobSaleDetailOrderInfoList) {
        // 预售单提交后，冻结库存，
        List<MateInventoryLockLogAddRequest> requests = new ArrayList<>();
        for (TobSaleDetailOrderInfo tobSaleDetailOrderInfo : tobSaleDetailOrderInfoList) {
            MateInventoryLockLogAddRequest mateInventoryLockLogAddRequest = new MateInventoryLockLogAddRequest();
            mateInventoryLockLogAddRequest.setOrderType(LogisticsTraceDetailOrderTypeEnum.T2.getIntValue());
            mateInventoryLockLogAddRequest.setSourceOrderNo(tobSaleDetailOrderInfo.getOrderNo());
            mateInventoryLockLogAddRequest.setSourceDetailOrderNo(tobSaleDetailOrderInfo.getDetailOrderNo());
            mateInventoryLockLogAddRequest.setSourceOrderType(SubjectEnum.T42.getCode());
            mateInventoryLockLogAddRequest.setSubject(SubjectEnum.T42.getCode());
            mateInventoryLockLogAddRequest.setMateInventoryId(tobSaleDetailOrderInfo.getMateInventoryId());
            mateInventoryLockLogAddRequest.setLockQty(tobSaleDetailOrderInfo.getQty());
            requests.add(mateInventoryLockLogAddRequest);
        }
        mateInventoryFacade.lockMateInventory(requests);
    }

    public void tobRelease(TobSaleOrderInfo orderInfo, List<TobSaleDetailOrderInfo> tobSaleDetailOrderInfoList) {
        // 审批拒绝,释放库存
        List<MateInventoryLockLogAddRequest> requests = new ArrayList<>();
        for (TobSaleDetailOrderInfo tobSaleDetailOrderInfo : tobSaleDetailOrderInfoList) {
            MateInventoryLockLogAddRequest mateInventoryLockLogAddRequest = new MateInventoryLockLogAddRequest();
            mateInventoryLockLogAddRequest.setOrderType(LogisticsTraceDetailOrderTypeEnum.T2.getIntValue());
            mateInventoryLockLogAddRequest.setSourceOrderNo(tobSaleDetailOrderInfo.getOrderNo());
            mateInventoryLockLogAddRequest.setSourceDetailOrderNo(tobSaleDetailOrderInfo.getDetailOrderNo());
            mateInventoryLockLogAddRequest.setSourceOrderType(SubjectEnum.T42.getCode());
            mateInventoryLockLogAddRequest.setSubject(SubjectEnum.T42.getCode());
            mateInventoryLockLogAddRequest.setMateInventoryId(tobSaleDetailOrderInfo.getMateInventoryId());
            mateInventoryLockLogAddRequest.setLockQty(tobSaleDetailOrderInfo.getQty());
            requests.add(mateInventoryLockLogAddRequest);
        }
        mateInventoryFacade.releaseLockMateInventory(requests);
    }

    public void tobApplyPass(TobSaleOrderInfo orderInfo, List<TobSaleDetailOrderInfo> tobSaleDetailOrderInfoList) {
        // 预售单审批通过后，创建待出商品清单
        LogisticsTraceApplyAddRequest addRequest = new LogisticsTraceApplyAddRequest();
        addRequest.setOrderType(LogisticsTraceDetailOrderTypeEnum.T2.getIntValue());
        addRequest.setSourceOrderNo(orderInfo.getOrderNo());
        addRequest.setSourceOrderType(SubjectEnum.T42.getCode());
        addRequest.setSubject(SubjectEnum.T42.getCode());
        addRequest.setCreateUserCode(orderInfo.getCreateUserCode());
        addRequest.setCreateUserName(orderInfo.getCreateUserName());
        addRequest.setCompanyCode(null);
        addRequest.setCompanyName(orderInfo.getInSubWarehouseName());
        addRequest.setLock(false);
        addRequest.setSaleCkItems(tobSaleDetailOrderInfoList.stream().map(detailInfo -> {
            LogisticsTraceApplyAddRequest.SaleCkItem saleCkItem = new LogisticsTraceApplyAddRequest.SaleCkItem();
            saleCkItem.setSourceDetailOrderNo(detailInfo.getDetailOrderNo());
            saleCkItem.setMateInventoryId(detailInfo.getMateInventoryId());
            saleCkItem.setApplyInQty(detailInfo.getQty());
            return saleCkItem;
        }).collect(Collectors.toList()));
        logisticsTraceApplyOrderFacade.addLogisticsTraceApplyOrder(addRequest);
    }
}
